package com.zhny.utils.http.stormax;

//import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;

/**
 * 基于{@link HttpURLConnection}的http请求方法
 *
 * @author Alex
 *
 */

public class HttpUtils {
    private static HttpURLConnection connection = null;

    /**
     * 建立链接，设置超时时间为8秒
     *
     * @param path 链接地址
     * @return 已经建立完成的链接
     */
    private static HttpURLConnection getConnection(String path){
        try {
            URL url = new URL(path);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(8000);
            connection.setReadTimeout(8000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 向服务器发送请求，默认编码为UTF-8
     * @param path 请求地址
     * @return 从服务器获取的json数据包，或者<code>TIMEOUT</code>超时
     */
    public static String sendPostMethod(String path){
        StringBuilder response = new StringBuilder();
        connection = getConnection(path);
        try {
            InputStream in = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
            String line;
            while((line=reader.readLine())!=null){
                response.append(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "TIMEOUT";
        } catch (SocketTimeoutException e){
//            Log.e("ERROR","链接超时");
            return "TIMEOUT";
        } catch (IOException e) {
            e.printStackTrace();
            return "TIMEOUT";
        } finally {
            if(connection != null){
                connection.disconnect();
            }
        }
        return response.toString();
    }

    /**
     *
     * @param uploadURL 服务器上传地址
     * @param json  需要发送的json数据包
     * @return 服务器状态信息，根据实际情况更改，或者将处理程序独立
     */
    public static String sendJson(String uploadURL,String json){
        connection = getConnection(uploadURL);
        try {
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type","application/json");
            connection.setRequestProperty("Charset","UTF-8");
            connection.setRequestProperty("Accept", "application/json");
            OutputStream out = connection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(json);
            writer.flush();
            writer.close();
            if (connection.getResponseCode() == 200){
                InputStream in = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String flag ;
                while((flag = reader.readLine()) != null){
//                    方法内处理
//                    if(flag.equals("success")){
//                        return "success";
//                    }else if (flag.equals("fail")){
//                        return "fail";
//                    }else if (flag.equals("notFound")){
//                        return "notFound";
//                    }
                    //方法外处理
                    return flag;
                }
            }else {
                return "fail";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(connection != null){
                connection.disconnect();
            }
        }
        return "fail";
    }
}
